package com.DCapps.sendakiss.helper;

import java.util.ArrayList;

import com.DCapps.sendakiss.Friend;
import com.google.android.gcm.GCMRegistrar;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SQLHelper {

	private SQLiteDatabase database;
	private MySQLiteHelper dbHelper;
	Context c;

	public SQLHelper(Context c) {
		this.c = c;
	}

	public void open() throws SQLException {
		if (dbHelper == null)
			dbHelper = new MySQLiteHelper(c);
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public void setFriends(ArrayList<Friend> fs) {
		
		database.delete(MySQLiteHelper.TABLE_FRIENDS, null, null);
		open();
		
		ContentValues values = new ContentValues();
		for (Friend friend : fs) {
			values.put(MySQLiteHelper.COLUMN_NAMES, friend.getName());
			values.put(MySQLiteHelper.COLUMN_NMBRS, friend.getPhonenumber());
			values.put(MySQLiteHelper.COLUMN_GCMID, friend.getGCMID());
		}

		long insertId = database.insert(MySQLiteHelper.TABLE_FRIENDS, null,
				values);
	}

	public ArrayList<Friend> getFriends() {
		ArrayList<Friend> ListFriends = new ArrayList<Friend>();

		Cursor cursor = null;
		try {
			if (database == null)
				open();

			cursor = database.query(MySQLiteHelper.TABLE_FRIENDS, null, null,
					null, null, null, null);
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Friend comment = cursorToFriend(cursor);
			ListFriends.add(comment);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();

		Friend f = new Friend();
		f.setName("Demo");
		f.setId(01);
		f.setPhoneNumber("00000");
		f.setGCMID(GCMRegistrar.getRegistrationId(c));
		ListFriends.add(f);

		return ListFriends;
	}

	private Friend cursorToFriend(Cursor cursor) {
		Friend f = new Friend();
		f.setId(cursor.getLong(0));
		f.setName(cursor.getString(1));
		f.setPhoneNumber(cursor.getString(2));
		f.setGCMID(cursor.getString(3));
		return f;
	}

	public class MySQLiteHelper extends SQLiteOpenHelper {

		public static final String TABLE_FRIENDS = "Tnames";
		public static final String COLUMN_ID = "_id";
		public static final String COLUMN_NAMES = "names";
		public static final String COLUMN_NMBRS = "nmbrs";
		public static final String COLUMN_GCMID = "gcmid";

		private static final String DATABASE_NAME = "users.db";
		private static final int DATABASE_VERSION = 2;

		// Database creation sql statement
		private static final String DATABASE_CREATE = "create table "
				+ TABLE_FRIENDS + "(" + COLUMN_ID
				+ " integer primary key autoincrement, " + COLUMN_NAMES
				+ " text not null, " + COLUMN_NMBRS + " text not null, "
				+ COLUMN_GCMID + " text not null);";

		public MySQLiteHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase database) {
			database.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(MySQLiteHelper.class.getName(),
					"Upgrading database from version " + oldVersion + " to "
							+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_FRIENDS);
			onCreate(db);
		}

	}

}
